﻿using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using CommonTools;
using shop.Model;
using CommonTools.Basic;

namespace shop.Bll.BasicInfo
{

	/// <summary>   </summary>
	public class JRefundFromBll : BasicBllParent, IFastQueryBll<JRefundFrom>
	{


		/// <summary>
		/// 查询
		/// </summary>
		/// <param name="code">查询代码</param>
		/// <param name="isDetails">是否明细</param>
		/// <param name="msg">错误消息</param>
		/// <returns></returns>
		public override IEnumerable<object> Query(string code, bool isDetails, out string msg)
		{
			msg = null;
			var list = DB.FSql.Select<JRefundFrom>().Where(s => s.IsDetails == isDetails && s.Isdel == false);
			if (string.IsNullOrEmpty(code) == false)
			{
				list = list.Where(a => a.Code.StartsWith(code));
			}

			return list.ToList();      //排序
		}




		#region 分组: 增删改

		/// <summary> 添加分类 </summary>
		public override bool AddGroup(object obj, IUser user, out string msg)
		{
			msg = null;
			if (!(obj is JRefundFrom model))
			{
				msg = Model.CN.Msg_InformationCannotBeBlank; // "辅助资料为空";
				return false;
			}

			model.ID = (int)(DB.FSql.Insert(model).ExecuteIdentity());


			DB.FSql.Insert(new CLogs()
			{
				Computer = user.ComputerInfo,
				DocId = model.ID.ToString(),
				DocAction = "添加分类",
				DocName = nameof(JRefundFrom),
				Note = $"代码:{model.Code},名称:{model.Name}",
			}).ExecuteAffrows();


			return true;
			 
		}

		/// <summary> 删除分类 </summary>
		public override bool DeleteGroup(object obj, IUser user, out string msg)
		{
			msg = null;
			if (!(obj is JRefundFrom model))
			{
				msg = Model.CN.Msg_InformationCannotBeBlank; // "辅助资料为空";
				return false;
			}

			if (DB.FSql.Select<JRefundFrom>()
				.Any(a => a.ID != model.ID && a.Code.StartsWith(model.Code)
											 && a.Isdel == false))     //检查分类下是否由明细
			{
				msg = CN.Msg_GroupHasItemsCannotDelete;  // "此分组下还有项目, 不能删除!";
			}
			else
			{
				DB.FSql.Delete<JRefundFrom>()
				  .Where(model)
				  .ExecuteAffrows();


				//日志
				DB.FSql.Insert(new CLogs()
				{
					Computer = user.ComputerInfo,
					DocId = model.ID.ToString(),
					DocAction = "删除分类",
					DocName = nameof(JRefundFrom),
					Note = $"代码:{model.Code},名称:{model.Name}",
				}).ExecuteAffrows();


				return true;


			}
			return false;
		}


		/// <summary> 修改分组 </summary>
		public override bool UpdateGroup(object obj, IUser user, out string err)
		{
			err = "";
			if (!(obj is JRefundFrom model))
			{
				err = Model.CN.Msg_InformationCannotBeBlank; // "辅助资料为空";
				return false;
			}

			var item = DB.FSql.Select<JRefundFrom>().Where(a => a.Isdel == false && a.Code == model.Code).ToOne();

			if (item != null)
			{
				item.Isdel = false;
				item.Name = model.Name;
				

				DB.FSql.Update<JRefundFrom>()
					.Set(a => a.Name, model.Name)
					.SetSource(model)
					.ExecuteAffrows();


				//日志
				DB.FSql.Insert(new CLogs()
				{
					Computer = user.ComputerInfo,
					DocId = item.ID.ToString(),
					DocAction = "修改分类",
					DocName = nameof(JRefundFrom),
					Note = $"代码:{item.Code},名称:{item.Name}",
				}).ExecuteAffrows();


				return true;
			}
			else
			{
				err = "错误, 没有找到此项目";
				return false;    //找不到内容, 可能已经被别人删除了
			}

		}

		#endregion


		#region 明细: 增删改


		/// <summary> 填加分类 </summary>
		public override bool Insert(object obj, IUser user, out string msg)
		{
			msg = null;
			if (!(obj is JRefundFrom model))
			{
				msg = Model.CN.Msg_InformationCannotBeBlank; // "辅助资料为空";
				return false;
			}
			model.IsDetails = true;

			//if (string.IsNullOrEmpty(model.Code))
			//{
			//	msg = CN.Msg_CodeCannotBeBlank;   // "资料代码不能为空";
			//	return false;
			//}
			if (string.IsNullOrEmpty(model.Name))
			{
				msg = CN.Msg_NameCannotBeBlank;   // "资料名称不能为空";
				return false;
			}
			if (this.GetByCode(model.Code) != null )
			{
				msg = CN.Msg_CodeCannotRepeat;    // "代码不能重复";
				return false;
			}

			var maxid = NumberHelper.GetMaxIntNum(nameof(JRefundFrom));
			model.Code = maxid;

			model.Isdel = false;
			model.IsDetails = true;

			var id = DB.FSql.Insert(model).ExecuteIdentity();
			model.ID = (int)id;

			DB.FSql.Insert(new CLogs()
			{
				Computer = user.ComputerInfo,
				DocId = model.ID.ToString(),
				DocAction = "添加",
				DocName = nameof(JRefundFrom),
				Note = $"代码:{model.Code},名称:{model.Name}",
			}).ExecuteIdentity();

			return true;

		}


		/// <summary> 修改 </summary>
		public override bool Update(object obj, IUser user, out string msg)
		{
			msg = null;
			if (!(obj is JRefundFrom model))
			{
				msg = Model.CN.Msg_InformationCannotBeBlank; // "辅助资料为空";
				return false;
			}
			if (model.ID <= 0)
			{
				msg = CN.Msg_IdCannotRepeat;   // "资料ID不能为空";
				return false;
			}
			//if (string.IsNullOrEmpty(model.Code))
			//{
			//	msg = CN.Msg_CodeCannotBeBlank;   // "资料代码不能为空";
			//	return false;
			//}
			if (string.IsNullOrEmpty(model.Name))
			{
				msg = CN.Msg_NameCannotBeBlank;   // "资料名称不能为空";
				return false;
			}

			DB.FSql.Update<JRefundFrom>()
			 .SetSource(model)
			 .ExecuteAffrows();


			//日志
			DB.FSql.Insert(new CLogs()
			{
				Computer = user.ComputerInfo,
				DocId = model.ID.ToString(),
				DocAction = "修改",
				DocName = nameof(JRefundFrom),
				Note = $"代码:{model.Code},名称:{model.Name}",
			}).ExecuteAffrows();

			return true;

		}


		/// <summary> 删除 </summary>
		public override bool Delete(object obj, IUser user, out string msg)
		{
			msg = null;
			if (!(obj is JRefundFrom model))
			{
				msg = CN.Msg_SystemError;
				return false;
			}
			if (model.ID <= 0)
			{
				msg = CN.Msg_IdCannotRepeat;   // "资料ID不能为空";
				return false;
			}

			model.Isdel = true;

			DB.FSql.Update<JRefundFrom>()
				.SetSource(model)
				.Set(a => a.Isdel, model.Isdel)
				.ExecuteAffrows();


			//日志
			DB.FSql.Insert(new CLogs()
			{
				Computer = user.ComputerInfo,
				DocId = model.ID.ToString(),
				DocAction = "删除",
				DocName = nameof(JRefundFrom),
				Note = $"代码:{model.Code},名称:{model.Name}",
			}).ExecuteAffrows();


			return true;


		}


		#endregion



		#region 实现IFastQueryBll 接口, 在FastBox中使用

		/// <summary> 在查询窗体,报表中显式的名称 </summary>
		public string Title => "银行账户";

		/// <summary> 本地存储的明细 </summary>
		/// <remarks>如果基础资每次都从数据库查询, 这个没用, 保持null</remarks>
		public List<JRefundFrom> Items => null;


		/// <summary> 是否每次从数据库查询 </summary>
		public bool QueryFromDataBase => true;

		
		/// <summary> 存储在快速查询窗体中,显示的属性,及属性的显示, 这个暂时不用,以后删除 </summary>
		public Dictionary<string, string> DisplayColumns => null;


		/// <summary> 显示的列, 静态 </summary>
		private static readonly string[] _columns = new string[] { nameof(JRefundFrom.Code), nameof(JRefundFrom.Name) };


		/// <summary> 存储在快速查询窗体中,显示的属性 </summary>
		public string[] Columns => _columns;


		/// <summary> 实体模型的代码属性名称 </summary>
		public string CodeField => nameof(JRefundFrom.Code);



		/// <summary> 代码中级别的分隔符 </summary>
		public override string Separator => ".";




		/// <summary> 查询树形图的所有节点 </summary>
		public override IEnumerable<IFastTree> QueryTreeList()
		{
			var rst = DB.FSql.Select<JRefundFrom>().Where(a => a.IsDetails == false && a.Isdel == false);

			return rst.ToList();
		}


		/// <summary> 查询方法 </summary>
		public List<JRefundFrom> Query(string findText, string prefixCode = null)
		{
			var list = DB.FSql.Select<JRefundFrom>()
				.Where(a => a.Isdel == false && a.IsDetails == true)
				.WhereIf(!string.IsNullOrEmpty(findText),a=> (a.Name.Contains(findText) || a.Code.Contains(findText) ))
				.WhereIf(!string.IsNullOrEmpty(prefixCode) ,a => a.Code.StartsWith(prefixCode));

			return list.ToList().OrderBy(a => a.Code).ToList();
		}


		/// <summary> 查询树形图的一个节点下的所有内容 </summary>
		public List<JRefundFrom> QueryByGroup(string group)
		{
			return DB.FSql.Select<JRefundFrom>()
				.Where(w => w.IsDetails == true && w.Isdel == false && w.Code.StartsWith(group))
				.ToList().OrderBy(a => a.Code).ToList();        //排序
		}


		#endregion


		#region 权限

		/// <summary> 查询导出权限 </summary>
		public override string LookRight => "01.32.1";

		/// <summary> 保存权限 </summary>
		public override string SaveRight => "01.32.2";

		/// <summary> 修改别人单据权限 </summary>
		public string ModifyRight => "01.32.3";

		/// <summary> 删除别人单据权限 </summary>
		public string DeleteRight => "01.32.4";

		/// <summary> 审核,反审核权限 </summary>
		public override string CheckRight => "01.32.5";
 
		#endregion



		#region 其他


		/// <summary> 使用代码查询一个 </summary>
		public JRefundFrom GetByCode(string code)
		{
			return DB.FSql.Select<JRefundFrom>()
				.Where(w => w.IsDetails == true && w.Isdel == false && w.Code == code).ToOne();
		}

		public JRefundFrom GetByNum(string num)
		{
			return DB.FSql.Select<JRefundFrom>()
				.Where(w => w.IsDetails == true && w.Isdel == false && (w.Name == num  )).ToOne();
		}

		#endregion



		#region 按钮,事件

		public override bool Check(object obj, IUser user, out string err)
		{
			throw new NotImplementedException();
		}

		public override bool UnCheck(object obj, IUser user, out string err)
		{
			throw new NotImplementedException();
		}



		#endregion

	}
}



 
